/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.univcasa.managing.impl;

import com.univcasa.db.model.CompteEtudiant;
import com.univcasa.db.model.ComptePersonnel;
import com.univcasa.db.model.Criteres;
import com.univcasa.db.model.MoodleFlag;
import com.univcasa.db.persist.HibernateUtil;
import com.univcasa.db.persist.MoodleHibernateUtil;
import com.univcasa.managing.MoodleDBManager;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author issam
 */
public class MoodleDBManagerImpl implements MoodleDBManager {

    private static Logger log = Logger.getLogger(MoodleDBManagerImpl.class);
        
    public void addStd2MoodleSync(CompteEtudiant std) {
        MoodleFlag flag = new MoodleFlag();
        flag.setId( std.getApogee() );
        flag.setType("std");

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.merge(flag);
        tx.commit();
        log.debug("SUCCESS ADD flag to DB " + flag.getId() );
    }

    public void addPers2MoodleSync(ComptePersonnel pers) {
        MoodleFlag flag = new MoodleFlag();
        flag.setId( pers.getSom() );
        flag.setType("prs");

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.merge(flag);
        tx.commit();
        log.debug("SUCCESS ADD flag to DB " + flag.getId() );
    }

    public void deleteStdFromMoodleSync(String apogee){
        MoodleFlag flag = getFlagById(apogee, "std");
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.delete( flag );
        tx.commit();
    }

    public void deleteStdFromMoodleSync(CompteEtudiant std) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void deletePersFromMoodleSync(ComptePersonnel std) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public MoodleFlag getFlagById(String id,String type){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "Select flag From MoodleFlag flag where flag.type=:type and id like :id " ;
        Query query = session.createQuery( hql );
        query.setString("type", type );
        query.setString("id", id );
        return (MoodleFlag) query.uniqueResult();
    }

    public List getListComptesMoodle(Criteres crits) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "Select flag From MoodleFlag flag where flag.type=:type and id like :apogee " ;
        Query query = session.createQuery( hql );
        query.setString("type", crits.getCritere("type") );
        query.setString("apogee", crits.getCritere("apogee") );
        List lista = query.list();
        return lista;
    }

    public boolean isExistInMoodle(String uid){

        Session session = MoodleHibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        String hql = "Select user From MoodleUser user where user.username=:uid" ;
        Query query = session.createQuery( hql );
        query.setString("uid", uid);

        return query.list().size()!=0 ;
    }

}
